package com.twu.biblioteca;

import java.util.ArrayList;

public class BibliotecaApp {

    private static UserInputHandler userInput = new UserInputHandler();

    public static void main(String[] args) {
        System.out.println("Hello, world!");

        printWelcomeMessage();

        User currentUser = attemptLogin();

        MainMenu menu = new MainMenu(currentUser);

        menu.initialise();



    }

    private static User attemptLogin() {

        String libraryCardNumber = askUserForLibraryCardNumber();
        String password = askUserForPassword();

        User potentialUser;

        while((potentialUser = validateCredentials(libraryCardNumber,password)) == null)
        {
            System.out.println("Invalid Login, please try again.");
            libraryCardNumber = askUserForLibraryCardNumber();
            password = askUserForPassword();
        }

        return potentialUser;
    }

    public static User validateCredentials(String libarayCardNumber, String passwrod)
    {

        ArrayList<User> allUsers = new MockUserDatabase().getAllUsers();

        for(int i =0; i < allUsers.size(); i++)
        {
            User workingUser = allUsers.get(i);
            if (libarayCardNumber.equals(workingUser.libraryCardNumber) && passwrod.equals(workingUser.password))
            {
                return workingUser;
            }
        }
        return null;

    }

    private static String askUserForLibraryCardNumber() {
        System.out.print("Please enter your Library Card Number: \t");
        return userInput.readInput();
    }



    private static String askUserForPassword()
    {
        System.out.print("Please enter your password: \t");
        return userInput.readInput();

    }


    private static void printWelcomeMessage() {

        System.out.println("Welcome to Biblioteca!");

    }

}
